/**
 * SEVAS - Swiss Electronic Vaccination Advisor Service / Agile Development
 *
 * $Id: Application.java 29 2010-08-18 16:51:14Z zimmr7@bfh.ch $
 */
package ch.bfh.sevas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class BaseVaccinationCatalog {

    private List<RecommendedBaseVaccination> vaccinations;
    private static BaseVaccinationCatalog theCatalog;

    public static BaseVaccinationCatalog getFilledBaseVaccineCatalog() {
        
        if (null == theCatalog) {
            /*
            "Diphtherie
            Tetanus
            Pertussis"                              DTP         Boostrix	GSK	0.5 ml
            Haemophilus influenzae Serotyp b	Hib         Hibtiter	Wyeth	0.5 ml
            Poliomyelitis                           IPV         Poloral	Berna	0.5 ml
            "Masern
            Mumps
            Röteln"                                 MMR         M-M-R-II	SPMSD	1 Stk
            Hepatitis B                             HBV         Heprecomb	Berna	0.5 ml
            Varizellen                              VZV         Varilrix	GSK	0.5 ml
            HPV                                     HPV         Gardasil	SPMSD	0.5 ml
            Influenza                               Infl        Influvac	Solvay Pharma	0.5 ml
            Pneumokokken                            Wind        Prevanar	Wyeth	0.5 ml
             */

            //this simulates a vaccine database

            Age a2m = new Age("2m", 61);
            Age a4m = new Age("4m", 122);
            Age a6m = new Age("6m", 183);
            Age a12m = new Age("12m", 366);
            Age a15m = new Age("15m", 458);
            Age a4j = new Age("4j", 1464);
            Age a11j = new Age("11j", 4026);

            RecommendedBaseVaccination DTP = new RecommendedBaseVaccination("Diphtheria, Tetanus, Pertussis", "DTP", "Boostrix", "GSK", "0.5 ml", Arrays.asList(a2m, a4m, a6m, a15m, a4j, a11j));
            RecommendedBaseVaccination Hib = new RecommendedBaseVaccination("Haemophilus influenzae type b", "Hib", "Hibtiter", "Wyeth", "0.5 ml", Arrays.asList(a2m, a4m, a6m, a15m));
            RecommendedBaseVaccination IPV = new RecommendedBaseVaccination("Poliomyelitis", "IPV", "Poloral", "Berna", "0.5 ml", Arrays.asList(a2m, a4m, a6m, a15m, a4j));
            RecommendedBaseVaccination MMR = new RecommendedBaseVaccination("Measles, Mumps, Rubella", "MMR", "M-M-R-II", "SPMSD", "1 pcs.", Arrays.asList(a12m, a15m));
            RecommendedBaseVaccination HBV = new RecommendedBaseVaccination("Hepatitis B", "HBV", "Heprecomb", "Berna", "0.5 ml", Arrays.asList(a11j));
            RecommendedBaseVaccination VZV = new RecommendedBaseVaccination("Varicella", "VZV", "Varilrix", "GSK", "0.5 ml", Arrays.asList(a11j));
            RecommendedBaseVaccination HPV = new RecommendedBaseVaccination("HPV", "HPV", "Gardasil", "SPMSD", "0.5 ml", Arrays.asList(a11j));
            RecommendedBaseVaccination Infl = new RecommendedBaseVaccination("Influenza", "Infl", "Influvac", "Solvay Pharma", "0.5 ml", Arrays.asList(a11j));
            RecommendedBaseVaccination Pneu = new RecommendedBaseVaccination("Pneumococcal", "Pneu", "Prevanar", "Wyeth", "0.5 ml", new ArrayList<Age>());

            theCatalog = new BaseVaccinationCatalog(Arrays.asList(DTP, Hib, IPV, MMR, HBV, VZV, HPV, Infl, Pneu));
        }

        return theCatalog;
    }

    public BaseVaccinationCatalog(List<RecommendedBaseVaccination> vaccines) {
        this.vaccinations = vaccines;
    }

    public List<RecommendedBaseVaccination> VaccinationsDue(Patient patient, Date dueDate) {
        List<RecommendedBaseVaccination> vaccList = new ArrayList<RecommendedBaseVaccination>();

        Age patientAge = new Age(patient.getDateOfBirth(), dueDate);

        for (int i = 0; i < vaccinations.size(); i++) {
            /// TODO: find number of already administered vaccinations
            if (vaccinations.get(i).IsVaccinationDue(patientAge, 0)) {
                vaccList.add(vaccinations.get(i));
            }
        }

        return vaccList;
    }

    public List<RecommendedBaseVaccination> getAllVaccinations() {
        return vaccinations;
    }

    public RecommendedBaseVaccination getVaccinationByCode(String code) {
        for (RecommendedBaseVaccination vacc : vaccinations) {
            if (vacc.getCode().equals(code)) {
                return vacc;
            }
        }
        throw new IllegalArgumentException("code " + code + " is not a valid vaccination code.");
    }
}
